/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  GeneralEditor
 * 
 * Description of class:
 * 
 * Uses the interface BasicEditor to control control flow of save and load
 * operations of editors.
 * 
 */

package karel.editors;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GeneralEditor {
    
    protected final JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
    protected String defaultFileExtention;
    private JFrame f;
    private BasicEditor e;
    private File currentFile;
    private boolean askForSave;
    
    /**
     * Constructor
     * @param parentFrame - Frame that displays dialog boxes.
     * @param editor - Editor to call on load/save operations
     */
    public GeneralEditor(JFrame parentFrame, BasicEditor editor) {
        f = parentFrame;
        e = editor;
        reset();
        askForSave = false;
        defaultFileExtention = "";
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 
        fc.removeChoosableFileFilter(fc.getFileFilter());
    }
    
    /**
     * Resets the general editor
     */
    private void reset() {
        currentFile = null;
    }
    
    /**
     * Tells GeneralEditor to ask to save file on exit/load.
     */
    public void askForSave() {
        askForSave = true;
    }
    
    /**
     * @return current file open in GeneralEditor.
     */
    public File getFile() {
        return currentFile;
    }
    
    /**
     * Control sequence when making a new file.
     */
    public void newFile() {
        if(askForSave) {
            while(true) {
                Object[] options = {"Já", "Nei", "Hætta við"};
                int n = JOptionPane.showOptionDialog(f,
                    "Viltu vista áður en þú býrð til nýja skrá?",
                    "Spurning!",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,     //do not use a custom Icon
                    options,  //the titles of buttons
                    options[0]); //default button title
                if(n == 0) {
                    if(save()) {
                        break;
                    }
                } else if (n == 1) {
                    break;
                } else {
                    return;
                }
            }
        }
        reset();
        e.newConcreteFile();
        askForSave = true;
    }
    
    /**
     * Control sequence when loading a file.
     */
    public void load() {
        if(askForSave) {
            while(true) {
                Object[] options = {"Já", "Nei", "Hætta við"};
                int n = JOptionPane.showOptionDialog(f,
                    "Viltu vista áður en þú opnar aðra skrá?",
                    "Spurning!",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,     //do not use a custom Icon
                    options,  //the titles of buttons
                    options[0]); //default button title
                if(n == 0) {
                    if(save()) {
                        break;
                    }
                } else if (n == 1) {
                    break;
                } else {
                    return;
                }
            }
        }
        int returnVal = fc.showOpenDialog(f);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            currentFile = fc.getSelectedFile();
            e.loadConcreteFile(currentFile);
            askForSave = true;
        }
    }
    
    /**
     * Control sequence when loading a file without FC.
     */
    public void load(File file) {
        if(askForSave) {
            while(true) {
                Object[] options = {"Já", "Nei", "Hætta við"};
                int n = JOptionPane.showOptionDialog(f,
                    "Viltu vista áður en þú opnar aðra skrá?",
                    "Spurning!",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,     //do not use a custom Icon
                    options,  //the titles of buttons
                    options[0]); //default button title
                if(n == 0) {
                    if(save()) {
                        break;
                    }
                } else if (n == 1) {
                    break;
                } else {
                    return;
                }
            }
        }
        currentFile = file;
        e.loadConcreteFile(currentFile);
        askForSave = true;
    }
    
    /**
     * Control sequence when saving a file.
     */
    public boolean save() {
        if(!e.isSavingAllowed()) {
            return false;
        }
        if(currentFile==null) {
            return saveAs();
        } else {
            e.saveConcreteFile(currentFile);
            askForSave = false;
            return true;
        }
    }
    
    /**
     * Control sequence when saving a nameless.
     */
    public boolean saveAs() {
        if(!e.isSavingAllowed()) {
            return false;
        }
        int returnVal = fc.showSaveDialog(f);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            if(!fc.getSelectedFile().getAbsolutePath().endsWith(defaultFileExtention)) {
                currentFile = new File(fc.getSelectedFile() + defaultFileExtention);
            } else {
                currentFile = fc.getSelectedFile();
            }
            if(currentFile.exists()) {
                Object[] options = {"Já", "Nei"};
                int n = JOptionPane.showOptionDialog(f,
                    "Viltu skrifa yfir '"+currentFile.getName()+"'?",
                    "Spurning!",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,     //do not use a custom Icon
                    options,  //the titles of buttons
                    options[0]); //default button title
                if(n != 0) {
                    reset();
                    return false;
                }
            }
            e.saveConcreteFile(currentFile);
            askForSave = false;
            return true;
        }
        return false;
    }
    
    /**
     * Control sequence when editor exits.
     */
    public void exit() {
        if(askForSave) {
            while(true) {
                Object[] options = {"Já", "Nei", "Hætta við"};
                int n = JOptionPane.showOptionDialog(f,
                    "Viltu vista áður en þú hættir?",
                    "Spurning!",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,     //do not use a custom Icon
                    options,  //the titles of buttons
                    options[0]); //default button title
                if(n == 0) {
                    if(save()) {
                        break;
                    }
                } else if (n == 1) {
                    break;
                } else {
                    return;
                }
            }
        }
        e.exit();
        askForSave = false;
    }
}
